package com.ruige.goreka.service.impl;

import com.ruige.goreka.config.VersionConfig;
import com.ruige.goreka.dao.GorekaMapper;
import com.ruige.goreka.pojo.StatusCode;
import com.ruige.goreka.service.GorekaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.security.auth.login.Configuration;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class GorekaServiceImpl implements GorekaService {

    @Autowired
    private GorekaMapper gorekaMapper;

    @Autowired
    private VersionConfig versionConfig;


    /**
     * 查询的所有服务
     * @return
     */
    @Override
    public List<com.ruige.goreka.pojo.Service> findAll() {
        List<com.ruige.goreka.pojo.Service> serviceList = gorekaMapper.selectAll();
        return serviceList;
    }


    /**
     * 初始化服务数据,删除所有!
     */
    @PostConstruct
    @Override
    public void deleteAll() {
        System.out.println(new Date().toString()+"INFO: 正在初始化服务,删除所有服务信息!");
        try {
            gorekaMapper.deleteAll();
            System.out.println(new Date().toString()+"  INFO: 初始化成功!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(new Date().toString()+"  ERROR: 初始化失败!!");
        }
    }

    /**
     * 注册服务
     * @param service
     */
    @Override
    public void add(com.ruige.goreka.pojo.Service service) {
        com.ruige.goreka.pojo.Service result_service = gorekaMapper.findByServiceName(service.getService_name());
        //判断当前服务是否存在
        if (result_service == null || "".equals(result_service.getService_name())) {
            //不存在,设置当前服务注册时间和状态
            Date current_time = new Date();
            service.setRegister_time(current_time);
            service.setLast_update_time(current_time);
            service.setStatus(StatusCode.STATUS_OK);
            gorekaMapper.insert(service);
        }
    }

    /**
     * 获取版本信息
     * @return
     */
    @Override
    public VersionConfig getVersion() {
        return versionConfig;
    }

    /**
     * 剔除服务
     * @param service
     */
    @Override
    public void delete(com.ruige.goreka.pojo.Service service) {
        gorekaMapper.deleteByPrimaryKey(service.getId());
    }

    /**
     * 根据服务名称更新服务状态
     * @param service
     */
    @Override
    public void updateStatusByServiceName(com.ruige.goreka.pojo.Service service) {
        gorekaMapper.updateStatusByServiceName(service);
    }

    /**
     * ping更新服务
     * @param service
     */
    @Override
    public void update(com.ruige.goreka.pojo.Service service) {
        service.setStatus(StatusCode.STATUS_OK);
        service.setLast_update_time(new Date());
        gorekaMapper.updateByServiceName(service);
    }

    /**
     * 根据名称查找服务
     * @param serviceName
     * @return
     */
    @Override
    public com.ruige.goreka.pojo.Service findByServiceName(String serviceName) {
        return gorekaMapper.findByServiceName(serviceName);
    }



}
